package com.lh.item.service.impl;

import com.lh.item.mapper.SpecGroupMapper;
import com.lh.item.mapper.SpecParamMapper;
import com.lh.item.pojo.SpecGroup;
import com.lh.item.pojo.SpecParam;
import com.lh.item.service.ISpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 规格信息管理
 *
 * @author liuhe
 * @date 2020/06/23
 */
@Service
public class SpecificationServiceImpl implements ISpecificationService {

    final SpecParamMapper specParamMapper;
    final SpecGroupMapper specGroupMapper;

    @Autowired
    public SpecificationServiceImpl(SpecParamMapper specParamMapper, SpecGroupMapper specGroupMapper) {
        this.specParamMapper = specParamMapper;
        this.specGroupMapper = specGroupMapper;
    }

    @Override
    public List<SpecParam> queryParams(Long gid, Long cid, Boolean generic, Boolean searching) {
        SpecParam record = new SpecParam();
        record.setGroupId(gid);
        record.setCid(cid);
        record.setGeneric(generic);
        record.setSearching(searching);
        return this.specParamMapper.select(record);
    }

    @Override
    public List<SpecGroup> querySpecGroup(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        return this.specGroupMapper.select(specGroup);
    }

    @Override
    public List<SpecGroup> querySpecsByCid(Long cid) {
        List<SpecGroup> specGroups = querySpecGroup(cid);

        specGroups.forEach(specGroup1 -> {
            specGroup1.setParams(this.queryParams(specGroup1.getId(), null, null, null));
        });
        return specGroups;
    }
}
